/*
 * Copyright (C) 2011 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.bkkit.redlung;

import java.io.File;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class RedLungPlugin extends JavaPlugin {
	private RadiatedPlayers radiatedPlayers;
	private Config config;
	private int taskID = -1;

	@Override
	public void onDisable() {
		this.radiatedPlayers.disable();
		this.config.disable();
		if (this.taskID != -1) {
			getServer().getScheduler().cancelTask(this.taskID);
		}
	}

	@Override
	public void onEnable() {
		config = new Config(getConfiguration());
		config.enable();
		PluginManager pm = getServer().getPluginManager();
		final PluginDescriptionFile descriptionFile = getDescription();
		LogWrapper.init(descriptionFile, config);

		final File datafolder = getDataFolder();
		File dataFile = new File(datafolder, "sick_players.yml");
		this.radiatedPlayers = new RadiatedPlayers(config, dataFile);
		this.radiatedPlayers.enable();

		final BlockListener blockListener = new BlockListener(config,
				this.radiatedPlayers);
		pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
				Event.Priority.Low, this);
		pm.registerEvent(Event.Type.BLOCK_DAMAGE, blockListener,
				Event.Priority.Low, this);

		final PlayerListener playerListener = new PlayerListener(
				this.radiatedPlayers, this.config);
		pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener,
				Event.Priority.Low, this);
		pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener,
				Event.Priority.Low, this);
		pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener,
				Event.Priority.Low, this);
		this.taskID = getServer().getScheduler().scheduleSyncRepeatingTask(
				this, playerListener, 500, 500);
		if (this.taskID == -1) {
			LogWrapper
					.info("Unable to schedule task, players will not be hurt by radiation");
		}
		LogWrapper.info("Enabled");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command,
			String label, String[] args) {
		if (args != null) {
			if (args.length > 0) {
				final String rlCommand = args[0];
				LogWrapper.fine("Sender" + sender + " command " + rlCommand
						+ "label " + label);
				if (rlCommand.equalsIgnoreCase("list")) {
					if (sender.hasPermission("redlung.list")) {
						if (sender instanceof Player) {
							int accumulatedAmount = this.radiatedPlayers
									.getAccumulatedAmount((Player) sender);
							sender.sendMessage("Accumulated radiation: "
									+ accumulatedAmount);
						} else {
							sender.sendMessage("You are not a player");
						}
					} else {
						sender.sendMessage("You do not have the redlung.list permission");
					}
					return true;
				} else if (rlCommand.equalsIgnoreCase("setlevel")) {
					if (args.length > 1) {
						try {
							final int level = Integer.parseInt(args[1]);
							if (sender.hasPermission("redlung.setlevel")) {
								Player player = null;
								if (args.length > 2) {
									String playerName = args[2];
									player = getServer().getPlayer(playerName);
								} else {
									if (sender instanceof Player) {
										player = (Player) sender;
									}
								}
								if (player == null) {
									sender.sendMessage("Player can not be found (maybe offline)");
								} else {
									this.radiatedPlayers.setRadiationLevel(
											player, level);
								}
							} else {
								sender.sendMessage("You do not have the redlung.setlevel permission");
							}
							return true;
						} catch (NumberFormatException e) {
							LogWrapper.fine("Unable to parse level argument: "
									+ args[1]);
						}
					}
				}
			}
		}
		// return false to indicate we did not understand the command
		return false;
	}
}
